<!DOCTYPE html>
<html lang="en" style="height:100%;">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>矿机市场</title>

    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/yzl.css"/>

</head>
<body style="height:100%;background-color:white;" >
<!-- <div id="blank" class="header" v-cloak>
    <div v-back.tapmode class="back"></div>
    <span class="title">矿机市场</span>
</div> -->
<div id="app"  style="background-color:#fff" v-cloak>
    <div class="machine_top">
        <div class="tab tab1" v-myclick="[1, changetab]" :class="{machineactive:tabindex==1}">矿机市场</div>
        <div class="tab tab2" v-myclick="[2, changetab]" :class="{machineactive:tabindex==2}">我的矿机</div>
        <div style="clear: both;"></div>
    </div>
    <div style="width: 90%;  margin: 0 auto;text-align: left;" v-if="tabindex==2">
        <div class="machinetotal-left">
            <div class="machinetotal" v-if="tabindex==2&&lists.length>0">
                总矿机数:{{lists.length}}&nbsp;&nbsp;总算力:{{totalpower}}M&nbsp;&nbsp;待开采:{{unminer}}
            </div>
            <div class="machinetotal" v-if="tabindex==2&&lists.length>0">
                总收益:{{totalincome}}&nbsp;&nbsp;今日收益:{{todayincome}}
            </div>
        </div>
        <div class="machinetotal-right" v-myclick="getincometotal">
            <div class="machinetotal1" v-if="tabindex==2&&lists.length>0">
                一键
            </div>
            <div class="machinetotal1" v-if="tabindex==2&&lists.length>0">
                收取
            </div>
        </div>
        <div style="clear:both"></div>
    </div>
    <div class="meachine-row" v-for="(detail,index) in lists" :key="index"  :style="{background:detail.color}">
        <div class="machine-left">
            <div class="machine_name">{{detail.name}}</div>
            <div class="machine_line" v-if="tabindex==1">运行周期：{{detail.days}} 天</div>
            <div class="machine_line" v-if="tabindex==2">待收取：{{detail.ungetget.toFixed(2)}} CCB</div>
            <div class="machine_line" v-if="tabindex==2">已运行：{{detail.alredydays}} 天</div>
            <div class="machine_line" v-if="tabindex==2">日收益：{{detail.profit}} CCB</div>
            <div class="machine_line" v-if="tabindex==1">周期产量：{{(detail.days*detail.profit).toFixed(0)}} CCB</div>
            <div class="machine_line" v-if="tabindex==1">年化率：{{detail.profitrate}}</div>
        </div>
        <div class="machine-right">
            <div class="machine_line" v-if="tabindex==1">价格：{{detail.price*1}} CC/{{detail.price2*1}} CCB</div>
            <div class="machine_line" v-if="tabindex==2">已收取：{{detail.income}} CCB</div>
            <div class="machine_line" v-if="tabindex==2">今日收益：{{detail.todayincome}} CCB</div>
            <div class="machine_line" v-if="tabindex==1">限购：{{detail.limit}} 台</div>
            <div class="machine_line" >算力：{{detail.power}}</div>
            <div class="statusbutton" v-if="tabindex==1" v-myclick="[detail, buymachine]">购买</div>
            <div class="statusbutton" v-if="tabindex==2&&!detail.ungetget">运行中</div>
            <div class="statusbutton" v-if="tabindex==2&&detail.ungetget" v-myclick="[detail, getincome]">领取收益</div>
        </div>
        <div style="clear:both"></div>
    </div>
    <div class="machinetip" v-if="tabindex==2&&lists.length==0">您还没有矿机，可以去市场买一台哦！</div>

    <div class="machinetip" v-if="tabindex==2">说明：次日凌晨可领取本日收益</div>
</div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/app.js"></script>
<script src="../../script/common.js"></script>
<script src="../../script/url.js"></script>
<script src="../../script/components.js"></script>

<script>
    var lists;
    var vn;
    var vm;
    apiready = function () {
        showProgress();
        setBSImg();
        vn = new Vue({
            el:'#blank',
            data:{
            },
        });

        var data = {
            member_id: getPrefs('member_id')
        };
        var callback = function (ret) {
            lists = ret.content;           
            initApp();
            api.parseTapmode();
        };
        pinet.post(URL.allmachine, data, callback);

        addListeners();
    };

    function initApp() {
        vm = new Vue({
            el:'#app',
            data:{
                lists:lists,
                tabindex:1,
                int:null,
                totalpower:0,
                unminer:0
            },
            methods:{
                buymachine:function(machine){
                    api.actionSheet({
                    title: '选择支付方式',
                    cancelTitle: '取消',
                    buttons: [machine.price*1+'CC', machine.price2*1+'CCB']
                }, function(ret, err) {
                    var index = ret.buttonIndex;
                    if(index==1){
                        vm.buyaction(1,machine.price*1+'CC',machine);
                    }
                    if(index==2){
                        vm.buyaction(2,machine.price2+'CCB',machine);
                    }
                });
                },
                buyaction:function(type,title,machine){
                    api.confirm({
						title: '提示',
						msg: '确定消耗'+title+'购买矿机？',
						buttons: ['确定', '取消']
					}, function (ret) {
						if (ret.buttonIndex == 1) {
                            showProgress();
                        var data = {
                            member_id: getPrefs('member_id'),
                            machine_id:machine.machine_id,
                            type:type
                        };
                        var callback = function () {
                            api.sendEvent({
                            name: 'upduser',
                        });
                            alert('购买成功！');
                        };
                        var errback = function (ret, err) {

                        };
                        pinet.post(URL.buymachine, data, callback, errback);         
                            }
					}); 
                },
                changetab: function (index) {
                    clearInterval(vm.int);
                    vm.lists = [];
                    if(index==1){
                        var data = {
                            member_id: getPrefs('member_id')
                        };
                        var callback = function (ret) {
                            vm.lists = ret.content;        
                        };
                        pinet.post(URL.allmachine, data, callback);
                    }
                    if(index==2){
                        var data = {
                            member_id: getPrefs('member_id')
                        };
                        var callback = function (ret) {
                            vm.lists = ret.content; 
                            vm.getprice();
                            vm.gettotal();
                            vm.circletime();           
                        };
                        pinet.post(URL.mymachine, data, callback);
                    }
                    this.tabindex = index;

                },
                circletime(){
                    vm.int = setInterval(function () {
                        vm.getprice();

                    }, 1000);
                },
                getprice:function(){
                    var todayincome = 0;
                    var unminer = 0;
                    var totalincome = 0;
                    vm.lists.forEach(element => {
                            var timestramp = new Date().getTime();
                            var persecond = element.profit/86400000;
                            var today = new Date().setHours(0, 0, 0, 0);
                            var objtime =  element.date_create*1000>today?element.date_create*1000:today
                            var value =(timestramp - objtime)*persecond;
                            // element.timeincome =  (value+element.income*1).toFixed(4);
                            element.todayincome =  value.toFixed(5);
                            unminer += (element.days - element.alredydays)*element.profit;
                            todayincome += Number(element.todayincome);
                            totalincome += Number(element.income)
                        });
                        vm.todayincome = todayincome.toFixed(5);
                        vm.unminer = unminer.toFixed(5);
                        vm.totalincome = totalincome.toFixed(2);
                },
                gettotal(){
                    var totalpower = 0;
                    vm.lists.forEach(element => {
                        var powerarr = element.power.split("M");
                        totalpower += Number(powerarr[0]);
                        });  
                        vm.totalpower = totalpower;
                },
                getincome:function(machine){
                    var data = {
                            member_id: getPrefs('member_id'),
                            machine_id: machine.machine_id,
                            mp_id: machine.mp_id,

                        };
                        var callback = function (ret) {
                            alert('领取成功！');
                            api.sendEvent({
                            name: 'upduser',
                        });
                            vm.changetab(2)
                        };
                        pinet.post(URL.setmachineaward, data, callback);
                },
                getincometotal(){
                    var data = {
                            member_id: getPrefs('member_id'),
                        };
                        var callback = function (ret) {
                            alert('领取成功！');
                            api.sendEvent({
                            name: 'upduser',
                        });
                            vm.changetab(2)
                        };
                        pinet.post(URL.getincometotal, data, callback);
                },
                getState: function (state) {
                    switch (state) {
                        case '1':
                            return '申请中';
                        case '2':
                            return '已通过';
                        case '3':
                            return '未通过';

                    }
                }
            }
        })
    }

    function addListeners() {
        api.addEventListener({
            name: 'viewappear'
        }, function () {
            onPageStart();
        });

        api.addEventListener({
            name: 'viewdisappear'
        }, function (ret, err) {
            onPageEnd();
        });

        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            onPageEnd();
            api.closeWin();
        });
    }

</script>
</html>